package com.office.shop.by.components;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.StaticLayout;
import android.util.Log;
import org.apache.http.NameValuePair;

import java.security.Identity;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Олег Семенов
 * Date: 05.07.11
 * Time: 10:57
 * To change this template use File | Settings | File Templates.
 */
public class CCache{

    private static String sharedPreferencesTag = CConfig.mainPackage+".MultiAccActivity";
    private static String cacheTimeTag = "time_";
    //private static

    public static String generateCacheId(String url, List<NameValuePair> paramList)
    {
        String cacheId = url.replace("http://","").replace("/","_");
        if(paramList != null)
        {
            for (int i = 0; i< paramList.size(); i++)
               cacheId += "_"+paramList.get(i).getValue();
        }
        return cacheId;
    }

    public static void putString(String id, String data)
    {
        SharedPreferences sharedPreferences = CAccounts.Context.getSharedPreferences(CConfig.mainPackage+".MultiAccActivity", 0);
        sharedPreferences.edit().putString(id,data).commit();
        sharedPreferences.edit().putLong(CCache.cacheTimeTag+id,System.currentTimeMillis()).commit();
    }

    public static String getString(String id)
    {
        SharedPreferences sharedPreferences = CAccounts.Context.getSharedPreferences(CConfig.mainPackage+".MultiAccActivity", 0);
        long saveTime =  sharedPreferences.getLong(CCache.cacheTimeTag + id, System.currentTimeMillis());

        if(saveTime + CConfig.cacheTimeout >  System.currentTimeMillis())
            return sharedPreferences.getString(id,null);
        else
           return null;
    }




}
